<?php
require_once("$sysroot/lib/3rdparty/class.phpmailer.php");

class TdMailer {
	
	private $name;
	private $host;
	private $port;
	private $username;
	private $password;
	private $mailer;
	private $from = array();
	private $to = array();
	private $subject;
	private $body;
	
	function __construct($name, $host = "", $port = "", $username = "", $password = "") {
		global $teardrop;
		
		$this->setName($name);
		
		if (isset($name) && $name != "" && isset($teardrop->config['mailer'][$name])) {
			$host = $teardrop->config['mailer'][$name]['host'];
			$port = $teardrop->config['mailer'][$name]['port'];
			$username = $teardrop->config['mailer'][$name]['username'];
			$password = $teardrop->config['mailer'][$name]['password'];
			
			$this->setFrom($teardrop->config['mailer'][$name]['from_mail'], $teardrop->config['mailer'][$name]['from_name']);
		}
		
		$this->setHost($host);
		$this->setPort($port);
		$this->setUsername($username);
		$this->setPassword($password);
		
		$this->mailer = new PHPMailer();
	}
	
	function send() {
		$this->mailer->IsSMTP();
		$this->mailer->Host = $this->host;
		$this->mailer->Port = $this->port;
		$this->mailer->SMTPAuth = true;
		$this->mailer->Username = $this->username;
		$this->mailer->Password = $this->password;

		$this->mailer->From = $this->from['mail'];
		$this->mailer->FromName = $this->from['name'];
		
		foreach ($this->to as $mail => $name) {
		  $this->mailer->AddAddress($mail, $name);
		}
		
		$this->mailer->IsHTML(true);
		$this->mailer->WordWrap = 50;
		$this->mailer->Subject = $this->subject;
		$this->mailer->Body = $this->body;
		
		return $this->mailer->Send();
	}
	
	function setName($value) {
		$this->name = $value;
	}
	function getname() {
		return $this->name;
	}
	
	function setFrom($mail, $name = "") {
		$this->from['mail'] = $mail;
		$this->from['name'] = $name;
	}
	function getFrom() {
		return $this->from;
	}
	
	function addTo($mail, $name = "") {
		$this->to[$mail] = $name;
	}
	function setTo($to) {
		$this->to = $to;
	}
	function getTo() {
		return $this->to;
	}
	
	function setSubject($value) {
		$this->subject = $value;
	}
	function getSubject() {
		return $this->subject;
	}	

	function setBody($value) {
		$this->body = $value;
	}
	function getBody() {
		return $this->body;
	}
	
	function setHost($value) {
		$this->host = $value;
	}
	function getHost() {
		return $this->host;
	}
	
	function setPort($value) {
		$this->port = $value;
	}
	function getPort() {
		return $this->port;
	}
	
	function setUsername($value) {
		$this->username = $value;
	}
	function getUsername() {
		return $this->username;
	}
	
	function setPassword($value) {
		$this->password = $value;
	}
	function getPassword() {
		return $this->password;
	}
}
?>